package com.smooth.site.sx.pc.job.service;

import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.util.HtmlUtils;

import com.smooth.common.site.common.enums.SiteCodeEnums;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.utils.StringUtils;
import com.smooth.dao.gen.BizJobMapper;
import com.smooth.dao.gen.bean.BaseCities;
import com.smooth.dao.gen.bean.BizCompany;
import com.smooth.dao.gen.bean.BizJob;
import com.smooth.dao.gen.bean.BizJobExample;
import com.smooth.dao.gen.bean.BizStudent;
import com.smooth.site.sx.mag.basedata.city.service.BaseCitiesService;
import com.smooth.site.sx.mag.biz.compcollege.comp.service.PcCollegeCompService;
import com.smooth.site.sx.mag.biz.job.service.BizJobService;
import com.smooth.site.sx.mag.biz.student.service.BizStudentService;
import com.smooth.site.sx.pc.job.enums.JobStatus;
import com.smooth.site.sx.pc.job.req.invite.InviteDetailReq;
import com.smooth.site.sx.pc.job.req.invite.InvitePubUpload;
import com.smooth.site.sx.pc.job.req.invite.InviteQueryReq;
import com.smooth.site.sx.pc.job.req.invite.InviteReq;
import com.smooth.site.sx.pc.job.req.invite.StuInviteQueryReq;
import com.smooth.sys.pc.vo.ListRsp;

@Component
public class PcJobService {
	@Autowired
	BizJobService bizJobService;
	@Autowired
	BaseCitiesService baseCitiesService;
	@Autowired
	BizJobMapper bizJobMapper;
	public BizJob addJob(InviteReq inviteReq,BizCompany secBizCompany) {
		BizJob bizJob = new BizJob();
		
		BeanUtils.copyProperties(inviteReq, bizJob);
		bizJob.setJobTitle(inviteReq.getJobTitle());
		
		BaseCities baseCities = baseCitiesService.get(inviteReq.getCityId());
		
		
		bizJob.setCityId(baseCities.getId());
		bizJob.setCityName(baseCities.getCityName());
		bizJob.setProvinceId(baseCities.getProvinceId());
		bizJob.setProvinceName(baseCities.getProvinceName());
		bizJob.setCollegeLevel(inviteReq.getCollegeLevel());
		bizJob.setJobDetail(inviteReq.getJobDetail());
		if(inviteReq.getJobStatus()==null) {
			bizJob.setJobStatus(JobStatus.published.value);
		}else {
			JobStatus jobStatus = JobStatus.getStatus(inviteReq.getJobStatus());
			bizJob.setJobStatus(jobStatus.value);
		}
		bizJob.setCompId(secBizCompany.getId());
		if(StringUtils.isBlank(inviteReq.getJobDetail())) {
			bizJob.setCompInfo(secBizCompany.getDetail());
		}
		bizJob.setCompName(secBizCompany.getCompName());
		if(StringUtils.isBlank(inviteReq.getCompAddress())) {
			bizJob.setCompAddress(secBizCompany.getCompAddress());
		}
		
		if(StringUtils.isNotBlank(inviteReq.getId())) {
			bizJob.setId(inviteReq.getId());
		}
		bizJob = bizJobService.save(bizJob);
		convertJobContent(bizJob);
		return bizJob;
	}
	
	public static void convertJobContent(List<BizJob> bizJobList) {
		for(BizJob bizJob : bizJobList) {
			convertJobContent(bizJob);
		}
	}
	public static void convertJobContent(BizJob bizJob) {
		if(StringUtils.isNotBlank(bizJob.getJobDetail())) {
			String jobDetail = StringEscapeUtils.unescapeHtml4(bizJob.getJobDetail());
			jobDetail = htmlReplace(jobDetail);
			bizJob.setJobDetail(jobDetail);
		}
	}
	
	public static String htmlReplace(String str){
		String s2 = HtmlUtils.htmlUnescape(str); 
        return s2;
    }
	public BizJob updJobStatus(InvitePubUpload invitePubUpload,BizCompany secBizCompany ) {
		BizJob bizJobDb = bizJobService.get(invitePubUpload.getId());
		if(bizJobDb==null) {
			throw new SiteException(SiteCodeEnums.param_error, invitePubUpload.getId()+" 不存在");
		}
		if(!Objects.equals(bizJobDb.getCompId(), secBizCompany.getId())) {
			throw new SiteException(SiteCodeEnums.param_error, invitePubUpload.getId()+" 无权限");
		}
		BizJob bizJob = new BizJob();
		
		bizJob.setId(invitePubUpload.getId());
		JobStatus jobStatus = JobStatus.getStatus(invitePubUpload.getJobStatus());
		
		bizJob.setJobStatus(jobStatus.value);
		
		bizJob = bizJobService.save(bizJob);
		convertJobContent(bizJob);
		return bizJob;
	}
	
	public BizJob queryJobDetail(BizCompany secBizCompany,InviteDetailReq inviteDetailReq) {
		BizJobExample bizJobExample = new BizJobExample();
		BizJobExample.Criteria criteria = bizJobExample.createCriteria();
		
		criteria.andCompIdEqualTo(secBizCompany.getId());
		criteria.andIdEqualTo(inviteDetailReq.getJobId());
		List<BizJob> blist = bizJobMapper.selectByExample(bizJobExample);
		if(blist.size()>0) {
			convertJobContent(blist.get(0));
			return blist.get(0);
		}
		return null;
	}
	public ListRsp<BizJob> queryJob(BizCompany secBizCompany,InviteQueryReq inviteQueryReq) {
		ListRsp<BizJob> brsp = new ListRsp<BizJob>();
		
		BizJobExample bizJobExample = new BizJobExample();
		BizJobExample.Criteria criteria = bizJobExample.createCriteria();
		if(inviteQueryReq.getJobStatus()!=null) {
			JobStatus jobStatusEnum = JobStatus.getStatus(inviteQueryReq.getJobStatus());
			criteria.andJobStatusEqualTo(jobStatusEnum.value);
		}
		if(StringUtils.isNotBlank(inviteQueryReq.getCompId())) {
			criteria.andCompIdEqualTo(inviteQueryReq.getCompId());
		}
		criteria.andCompIdEqualTo(secBizCompany.getId());
		if(StringUtils.isNotBlank(inviteQueryReq.getQueryStr())) {
			criteria.andJobTitleLike("%"+inviteQueryReq.getQueryStr()+"%");
		}
		bizJobExample.setPage(new Page(inviteQueryReq.getOffset()/inviteQueryReq.getLimit(),inviteQueryReq.getLimit()));
		bizJobExample.setOrderByClause(" create_date desc ");
		List<BizJob> blist = bizJobMapper.selectByExample(bizJobExample);
		convertJobContent(blist);
		brsp.setDatalist(blist);
		brsp.setTsize(bizJobMapper.countByExample(bizJobExample));
		
		return brsp;
	}
	
	
	@Autowired
	BizStudentService bizStudentService;
	@Autowired
	PcCollegeCompService pcCollegeCompService;
	public ListRsp<BizJob> queryStuJobs(StuInviteQueryReq stuInviteQueryReq,BizStudent bizStudent) {
		ListRsp<BizJob> brsp = new ListRsp<BizJob>();
		
		BizJobExample bizJobExample = new BizJobExample();
		BizJobExample.Criteria criteria = bizJobExample.createCriteria();

		criteria.andJobStatusEqualTo(JobStatus.published.value);
		if(StringUtils.isNotBlank(stuInviteQueryReq.getQueryStr())) {
			criteria.andJobTitleLike("%"+stuInviteQueryReq.getQueryStr()+"%");
		}
		if(StringUtils.isNotBlank(stuInviteQueryReq.getCompId())) {
			criteria.andCompIdEqualTo(stuInviteQueryReq.getCompId());
		}
		
		if(StringUtils.isNotBlank(stuInviteQueryReq.getUid())) {
			bizStudent = bizStudentService.get(stuInviteQueryReq.getUid());
			if(bizStudent!=null && StringUtils.isNotBlank(bizStudent.getSchId())) {
				//开始查询权限配置逻辑
				List<String> dlist = pcCollegeCompService.querCompListByCollege(bizStudent.getSchId());
				if(dlist.size()>0) {
					criteria.andCompIdIn(dlist);
				}else {
					dlist.add("-1");
					criteria.andCompIdIn(dlist);
				}
			}
		}
		bizJobExample.setPage(new Page(stuInviteQueryReq.getOffset()/stuInviteQueryReq.getLimit(),stuInviteQueryReq.getLimit()));
		bizJobExample.setOrderByClause(" create_date desc ");
		List<BizJob> blist = bizJobMapper.selectByExample(bizJobExample);
		
		if(bizStudent==null) {
			for(BizJob bizJob : blist) {
				setBlackKeyInfo(bizJob);
			}
		}
		brsp.setDatalist(blist);
		brsp.setTsize(bizJobMapper.countByExample(bizJobExample));
		
		return brsp;
	}
	
	public static void setBlackKeyInfo(BizJob bizJob) {
		bizJob.setCompAddress("*******");
		bizJob.setCompName("*********");
		bizJob.setCompInfo("********");
		bizJob.setLinkInfo("********");
	}
}
